/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_QuickCharStatList
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-26 -- Cliff Simpkins: Initial Version
 **********************************************************************
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  public partial class WGR_QuickCharStatList : PortalModuleBase {
    List<WGR_Business.GuildEntry> _altToons;

    #region Public Properties
    public enum RosterLoadType {
      LoadUnknown = 0,
      LoadCharClass = 1,
      LoadCharProfession = 2,
      LoadCharReputation = 3,
      ShowAltChars = 4,
      LoadGuildRank = 5
    }
    public int ParentModuleID {
      get {
        try {
          return int.Parse(lblParentModuleID.Text);
        } catch {
          return -1;
        }
      }
      set {
        lblParentModuleID.Text = value.ToString();
      }
    }
    public int ParentPortalID {
      get {
        try {
          return int.Parse(lblParentPortalID.Text);
        } catch {
          return -1;
        }
      }
      set {
        lblParentPortalID.Text = value.ToString();
      }
    }
    public bool ShowMainsOnly {
      get {
        return chbShowMainsOnly.Checked;
      }
      set {
        chbShowMainsOnly.Checked = value;
      }
    }
    public int ListSize {
      get {
        return dgCharList.PageSize;
      }
      set {
        dgCharList.PageSize = value;
      }
    }
    public String DisplayTitle {
      get {
        return lblDisplayTitle.Text;
        }
      set {
        lblDisplayTitle.Text = value;
        }
      }
    public String Title {
      get {
        return lblSectionTitle.Text;
      }
      set {
        lblSectionTitle.Text = value;
        if (value.Trim() != "" && value.Trim().Length > 1) {
          imgSectionImage.ImageUrl = "images/IconLarge_" + value.Trim() + ".png";
          imgSectionImage.AlternateText = value.Trim() + " icon";
          }
        if (this.DisplayTitle.Trim() == "") {
          this.DisplayTitle = value.Trim();
          }
      }
    }

    public RosterLoadType LoadType {
      get {
        switch (lblSectionLoadType.Text) {
          case "alts":
            return RosterLoadType.ShowAltChars;
          case "class":
            return RosterLoadType.LoadCharClass;
          case "prof":
            return RosterLoadType.LoadCharProfession;
          case "rep":
            return RosterLoadType.LoadCharReputation;
          case "guild_rank":
            return RosterLoadType.LoadGuildRank;
          default:
            return RosterLoadType.LoadUnknown;
        }
      }
      set {
        switch (value) {
          case RosterLoadType.LoadGuildRank:
            lblSectionLoadType.Text = "guild_rank";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadCharClass:
            lblSectionLoadType.Text = "class";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.LoadCharProfession: 
            lblSectionLoadType.Text = "prof";
            imgSectionImage.Visible = true;
            return;
          case RosterLoadType.LoadCharReputation:
            lblSectionLoadType.Text = "rep";
            imgSectionImage.Visible = false;
            return;
          case RosterLoadType.ShowAltChars:
            lblSectionLoadType.Text = "alts";
            imgSectionImage.Visible = false;
            return;
          default:
            lblSectionLoadType.Text = "unknown";
            imgSectionImage.Visible = false;
            return;
        }
      }
    }

    public List<WGR_Business.GuildEntry> AltChars {
      set {
        this._altToons = value;
      }
    }
    #endregion
    #region Public Methods
    public String ReplaceBadModuleID(String urlToReplace) {
      return urlToReplace.Replace("mid/-1", "mid/" + this.ParentModuleID.ToString());
    }
    public String ClassIcon(String className) {
      return "images/IconSmall_" + className.ToString().ToLower() + ".png";
    }
    public String CharIcon(String race, String gender) {
      return "images/IconSmall_" + race.ToString().ToLower() + "-" + gender.ToString().ToLower() + ".png";
    }
    public String StatIcon(String profName) {
      switch (this.LoadType) {
        case RosterLoadType.LoadCharProfession:
          return "images/IconSmall_" + profName.ToString().ToLower() + ".png";
        default:
          return "images/IconSmall_.png";
      }
    }
    #endregion
    #region Protected Methods
    private void BindDataToDatagrid() {

      if (this.ParentModuleID > 0) {

        List<WGR_Business.QuickStatListItem> _rosterEntryList;

        switch(this.LoadType) {
          case RosterLoadType.LoadCharProfession:
            _rosterEntryList = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByProfession(this.ParentModuleID, this.ParentPortalID, this.Title, this.ShowMainsOnly);
            break;
          case RosterLoadType.LoadCharReputation:
            _rosterEntryList = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByStatEntryAndType(this.ParentModuleID, this.ParentPortalID, this.Title, DotNetWarcraft.WoWGuildRoster.Business.WarcraftStatTypes.Reputation, this.ShowMainsOnly);
            break;
          case RosterLoadType.LoadGuildRank:
            _rosterEntryList = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByGuildRank(this.ParentModuleID, this.ParentPortalID, int.Parse(this.Title), this.ShowMainsOnly);
            imgSectionImage.Visible = false;
            lblDisplayTitle.Visible = false;
            lblSectionSep.Visible = false;
            break;
          default:
            _rosterEntryList = new List<DotNetWarcraft.WoWGuildRoster.Business.QuickStatListItem>();
            break;
        }
        dgCharList.DataSource = _rosterEntryList;
        dgCharList.DataBind();

        if (dgCharList.PageCount == 1) {
          dgCharList.PagerStyle.Visible = false;
        } else { dgCharList.PagerStyle.Visible = true; }
      }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e) {
      try {
        if (!Page.IsPostBack) {
          this.BindDataToDatagrid();
        }
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }

    }

    #region Events - Data Grid
    protected void dgCharList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {

      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {
        if (e.Item.DataItem != null) {
          try {
            WGR_Business.QuickStatListItem _entry = (WGR_Business.QuickStatListItem)e.Item.DataItem;

            Label _charname = (Label)e.Item.FindControl("lblCharName");
            if (_entry.Primary_Char_Flag > 0) {
              if (_charname != null) {
                _charname.CssClass = "wgrCharListMainChar";
              }
            } else if (_entry.Alt_to_Main_ID > 0) {
              if (_charname != null) {
                _charname.CssClass = "wgrCharListAltChar";
              }
            } else {
            }
          } catch(Exception) {}
        }
      }

      System.Web.UI.WebControls.Label _lastLogin_Time = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblLastLogin_time");
      System.Web.UI.WebControls.Label _lastLogin_Label = (System.Web.UI.WebControls.Label)e.Item.FindControl("lblLastLogin_label");

      if (_lastLogin_Time != null)
        _lastLogin_Time.Visible = false;

      if (_lastLogin_Label != null)
        _lastLogin_Label.Visible = true;

      //if (_lastLogin_Time != null && _lastLogin_Label != null) {
      //  if (this.IsInOfficerRole) {
      //    _lastLogin_Time.Visible = true;
      //    _lastLogin_Label.Visible = false;
      //  } else {
      //    _lastLogin_Time.Visible = false;
      //    _lastLogin_Label.Visible = true;
      //  }
      //}
    }
    protected void dgCharList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) {
      dgCharList.CurrentPageIndex = e.NewPageIndex;
      this.BindDataToDatagrid();
    }

    #endregion
  }
}